﻿

$(document).ready(function () {
   
    //On blur
    $("#username").blur(validateName);
    $("#email").blur(validateEmail);
   
    //On key press
    $("#username").keyup(validateName);
    $("#comment").keyup(validateMessage);
    //On Submitting
//    form.submit(function () {
//        if (validateName() & validateEmail())
//            return true
//        else
//            return false;
//    });

    //validation functions

});
function validateEmail() {
    var email = $("#email");
    var emailInfo = $("#emailInfo");
    //testing regular expression
    var a = $("#email").val();
    var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
    //if it's valid email
    if (filter.test(a)) {
        email.removeClass("error");
        emailInfo.text("");
        emailInfo.removeClass("error");
        return true;
    }
    //if it's NOT valid
    else {
        email.addClass("error");
        emailInfo.text("Stop dude! Type a valid e-mail !");
        emailInfo.addClass("error");
        return false;
    }
}
function validateName() {
    var name = $("#username");
    var nameInfo = $("#nameInfo");
    //if it's NOT valid
    if (name.val().length < 4) {
        name.addClass("error");
        nameInfo.text("We want names with more than 3 letters!");
        nameInfo.addClass("error");
        return false;
    }
    //if it's valid
    else {
        name.removeClass("error");
        nameInfo.text("");
        nameInfo.removeClass("error");
        return true;
    }
}

function validateMessage() {
    var message = $("#comment");
    var commentInfo = $("#commentInfo");
   
    //it's NOT valid
    if (message.val().length < 10) {
        message.addClass("error");
        commentInfo.text("Put Your Thought Here !");
        return false;
    }
    //it's valid
    else {
        message.removeClass("error");
        commentInfo.text("");
        commentInfo.removeClass("error");
        return true;
    }
}